package com.weixin.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.weixin.exception.WeiXinPropertyNullPointException;
import com.weixin.exception.WeiXinSQLException;

class WeiXinDBBaseFactory {

	private static Logger logger = LogManager.getLogger();
	// Connection
	private static Connection connection = null;
	// weixinDB.Property 's file path
	private static String PATH = "/weixinDB.property";
	// weixinDB Properties Object
	private static Properties weixinProperties = null;

	static {
		try {
			weixinProperties = new Properties();
			InputStream in = WeiXinDBBaseFactory.class.getResourceAsStream(PATH);
			weixinProperties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("failed to get DB config from weixinDB.property , please check weixinDB.property");
			logger.error("catch exception : " + WeixinCommonUtil.getStackTrace(e));
			weixinProperties = null;
		}
	}

	/**
	 * 
	 * 
	 * @return
	 * @throws WeiXinPropertyNullPointException
	 * @throws WeiXinSQLException
	 */
	protected static Connection getConnection() throws WeiXinPropertyNullPointException, WeiXinSQLException {
		if (connection == null) {
			logger.debug("init connection");
			if (weixinProperties == null) {
				throw new WeiXinPropertyNullPointException("Not found weixinDB.property");
			}

			try {
//				logger.debug("username : " + weixinProperties.getProperty("Username"));
//				logger.debug("password : " + weixinProperties.getProperty("Password"));
//				logger.debug("URL : " + weixinProperties.getProperty("URL"));
//				logger.debug("dbDriver : " + weixinProperties.getProperty("dbDriver"));

				Class.forName(weixinProperties.getProperty("dbDriver"));
				connection = DriverManager.getConnection(weixinProperties.getProperty("URL").toString(),
						weixinProperties.getProperty("Username").toString(),
						weixinProperties.getProperty("Password").toString());
			} catch (SQLException e) {
				e.printStackTrace();
				throw new WeiXinSQLException("inputstream from weixinDB.property failed !");
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}

		return connection;
	}

	/**
	 * 关闭
	 * 
	 * @param conn
	 * @param ps
	 * @param rs
	 * @throws SQLException
	 */
	public static void close(Connection conn, PreparedStatement ps, ResultSet rs) throws SQLException {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw e;
			}
		}

		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw e;
			}
		}

		// 这里暂时不关闭connection
		/*
		 * if (conn != null) { try { conn.close(); } catch (SQLException e) {
		 * e.printStackTrace(); throw e; } }
		 */
	}

	public static void close(Connection conn, PreparedStatement ps) throws SQLException {
		close(conn, ps, null);
	}
}
